{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# MODIS Collections"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "ee.Initialize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from geetools import collection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ipygee as ui"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = ui.Map()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "8d1c643a41af4a5f972ac7244161c17a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Map(basemap={'max_zoom': 19, 'attribution': 'Map data (c) <a href=\"https://openstreetmap.org\">OpenStreetMap</a…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e86b155da79b434c80e329f7ae0ec57e",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Tab(children=(CustomInspector(children=(SelectMultiple(options=OrderedDict(), value=()), Accordion(selected_in…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "Map.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['MODIS/006/MOD09GQ',\n",
       " 'MODIS/006/MYD09GQ',\n",
       " 'MODIS/006/MOD09GA',\n",
       " 'MODIS/006/MYD09GA',\n",
       " 'MODIS/006/MOD13Q1',\n",
       " 'MODIS/006/MYD13Q1']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "collection.modis.IDS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "mod09ga = collection.MOD09GA()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "col = mod09ga.collection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "num_observations_1km (num_observations_1km) | scale: 1000\n",
      "state_1km (state_1km) | scale: 1000\n",
      "SensorZenith (sensor_zenith) | scale: 1000\n",
      "SensorAzimuth (sensor_azimuth) | scale: 1000\n",
      "Range (range) | scale: 1000\n",
      "SolarZenith (solar_zenith) | scale: 1000\n",
      "SolarAzimuth (solar_azimuth) | scale: 1000\n",
      "gflags (geolocation_flags) | scale: 1000\n",
      "orbit_pnt (orbit_pointer) | scale: 500\n",
      "granule_pnt (granule_pointer) | scale: 500\n",
      "num_observations_500m (num_observations_500m) | scale: 500\n",
      "sur_refl_b01 (red) | scale: 500\n",
      "sur_refl_b02 (nir) | scale: 500\n",
      "sur_refl_b03 (blue) | scale: 500\n",
      "sur_refl_b04 (green) | scale: 500\n",
      "sur_refl_b05 (swir3) | scale: 500\n",
      "sur_refl_b06 (swir) | scale: 500\n",
      "sur_refl_b07 (swir2) | scale: 500\n",
      "QC_500m (QC_500m) | scale: 500\n",
      "obscov_500m (observation_coverage_500m) | scale: 500\n",
      "iobs_res (obs_number) | scale: 500\n",
      "q_scan (q_scan) | scale: 250\n"
     ]
    }
   ],
   "source": [
    "for band in mod09ga.bands:\n",
    "    print('{} ({}) | scale: {}'.format(band.id, band.name, band.scale))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.addLayer(col.first(), mod09ga.visualization('NSR'), 'MOD09GA')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'QC_500m': ['B7_highest_quality',\n",
       "  'B5_highest_quality',\n",
       "  'B3_highest_quality',\n",
       "  'B1_highest_quality',\n",
       "  'B2_highest_quality',\n",
       "  'B4_highest_quality',\n",
       "  'B6_highest_quality'],\n",
       " 'state_1km': ['shadow',\n",
       "  'small_cirrus',\n",
       "  'average_cirrus',\n",
       "  'high_cirrus',\n",
       "  'adjacent',\n",
       "  'snow',\n",
       "  'clear',\n",
       "  'cloud',\n",
       "  'mix']}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mod09ga.bitOptions()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Masks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "col_masked = col.map(lambda img: mod09ga.applyPositiveMask(img, 'state_1km', ['clear']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.addLayer(col_masked.first(), mod09ga.visualization('NSR'), 'MOD09GA Masked')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "sr1 = mod09ga.getBand('sur_refl_b01')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'red'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sr1.name"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Vegetation index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "mod13q1 = collection.MOD13Q1()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "col13q1 = mod13q1.collection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'DetailedQA': ['adjacent',\n",
       "  'shadow',\n",
       "  'highest_qa',\n",
       "  'good_qa',\n",
       "  'snow',\n",
       "  'cloud'],\n",
       " 'SummaryQA': ['clear', 'marginal', 'snow', 'cloud']}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mod13q1.bitOptions()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "col13q1_mask = col13q1.map(lambda img: mod13q1.applyPositiveMask(img, 'SummaryQA', ['clear']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "Map.addLayer(col13q1.first(), dict(bands=['NDVI'], min=0, max=8000, palette=['brown', 'green']), 'MOD13Q1 NDVI Masked')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
